﻿//-----------------------------------------------------------------------
// <copyright>
// This file is subject to the terms and conditions of the Microsoft Public License (MS-PL).
// See http://www.microsoft.com/opensource/licenses.mspx#Ms-PL for more details.
// </copyright>
//----------------------------------------------------------------------- 
using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace Fulcrum.UnitTests.ControlGenerator
{
    [TestClass]
    public class ControlGeneratorXmlConfigurationTests
    {
        public ControlGeneratorXmlConfigurationTests()
        {
            // nothing
        }

        private const string StandardXml = "<ControlGenerator RoutedPropertyEventArgsFactory='RPEAF' TemplatePartFactory='TPF' OptionalTemplatePartFactory='OTPF' Company='company' Copyright='copyright' Namespace='namespace' > <Control name='one' /> <Control name='two' /> </ControlGenerator>";

        private const string EmptyRoutedPropertyEventArgsFactoryXml = "<ControlGenerator RoutedPropertyEventArgsFactory='' TemplatePartFactory='TPF' OptionalTemplatePartFactory='OTPF' Company='company' Copyright='copyright' Namespace='namespace' > <Control name='one' /> <Control name='two' /> </ControlGenerator>";
        private const string MissingRoutedPropertyEventArgsFactoryXml = "<ControlGenerator TemplatePartFactory='TPF' OptionalTemplatePartFactory='OTPF' Company='company' Copyright='copyright' Namespace='namespace' > <Control name='one' /> <Control name='two' /> </ControlGenerator>";

        private const string EmptyTemplatePartFactoryXml = "<ControlGenerator RoutedPropertyEventArgsFactory='RPEAF' TemplatePartFactory='' OptionalTemplatePartFactory='OTPF' Company='company' Copyright='copyright' Namespace='namespace' > <Control name='one' /> <Control name='two' /> </ControlGenerator>";
        private const string MissingTemplatePartFactoryXml = "<ControlGenerator RoutedPropertyEventArgsFactory='RPEAF' OptionalTemplatePartFactory='OTPF' Company='company' Copyright='copyright' Namespace='namespace' > <Control name='one' /> <Control name='two' /> </ControlGenerator>";

        private const string EmptyOptionalTemplatePartFactoryXml = "<ControlGenerator RoutedPropertyEventArgsFactory='RPEAF' TemplatePartFactory='TPF' OptionalTemplatePartFactory='' Company='company' Copyright='copyright' Namespace='namespace' > <Control name='one' /> <Control name='two' /> </ControlGenerator>";
        private const string MissingOptionalTemplatePartFactoryXml = "<ControlGenerator RoutedPropertyEventArgsFactory='RPEAF' TemplatePartFactory='TPF' Company='company' Copyright='copyright' Namespace='namespace' > <Control name='one' /> <Control name='two' /> </ControlGenerator>";

        private const string EmptyCompanyXml = "<ControlGenerator RoutedPropertyEventArgsFactory='RPEAF' TemplatePartFactory='TPF' OptionalTemplatePartFactory='OTPF' Company='' Copyright='copyright' Namespace='namespace' > <Control name='one' /> <Control name='two' /> </ControlGenerator>";
        private const string MissingCompanyXml = "<ControlGenerator RoutedPropertyEventArgsFactory='RPEAF' TemplatePartFactory='TPF' OptionalTemplatePartFactory='OTPF' Copyright='copyright' Namespace='namespace' > <Control name='one' /> <Control name='two' /> </ControlGenerator>";

        private const string EmptyCopyrightXml = "<ControlGenerator RoutedPropertyEventArgsFactory='RPEAF' TemplatePartFactory='TPF' OptionalTemplatePartFactory='OTPF' Company='company' Copyright='' Namespace='namespace' > <Control name='one' /> <Control name='two' /> </ControlGenerator>";
        private const string MissingCopyrightXml = "<ControlGenerator RoutedPropertyEventArgsFactory='RPEAF' TemplatePartFactory='TPF' OptionalTemplatePartFactory='OTPF' Company='company' Namespace='namespace' > <Control name='one' /> <Control name='two' /> </ControlGenerator>";

        private const string EmptyNamespaceXml = "<ControlGenerator RoutedPropertyEventArgsFactory='RPEAF' TemplatePartFactory='TPF' OptionalTemplatePartFactory='OTPF' Company='company' Copyright='copyright' Namespace='' > <Control name='one' /> <Control name='two' /> </ControlGenerator>";
        private const string MissingNamespaceXml = "<ControlGenerator RoutedPropertyEventArgsFactory='RPEAF' TemplatePartFactory='TPF' OptionalTemplatePartFactory='OTPF' Company='company' Copyright='copyright'  > <Control name='one' /> <Control name='two' /> </ControlGenerator>";

        private const string MissingControlsXml = "<ControlGenerator RoutedPropertyEventArgsFactory='RPEAF' TemplatePartFactory='TPF' OptionalTemplatePartFactory='OTPF' Company='company' Copyright='copyright' Namespace='namespace' > </ControlGenerator>";

        [TestMethod]
        public void TestCompany()
        {
            ControlGeneratorXmlConfigurationTestDouble.ConfigurationXml = StandardXml;
            var config = new ControlGeneratorXmlConfigurationTestDouble("thefile.xml");

            Assert.AreEqual("company", config.Company);
        }

        [TestMethod]
        public void TestControlConfigurations()
        {
            ControlGeneratorXmlConfigurationTestDouble.ConfigurationXml = StandardXml;
            var config = new ControlGeneratorXmlConfigurationTestDouble("thefile.xml");

            Assert.AreEqual(2, config.ControlConfigurations.Count);
            Assert.AreEqual("ControlName0", config.ControlConfigurations.ElementAt(0).Name);
            Assert.AreEqual("ControlName1", config.ControlConfigurations.ElementAt(1).Name);
        }

        [TestMethod]
        public void TestCopyright()
        {
            ControlGeneratorXmlConfigurationTestDouble.ConfigurationXml = StandardXml;
            var config = new ControlGeneratorXmlConfigurationTestDouble("thefile.xml");

            Assert.AreEqual("copyright", config.Copyright);
        }

        [TestMethod]
        public void TestFileName()
        {
            ControlGeneratorXmlConfigurationTestDouble.ConfigurationXml = StandardXml;
            var config = new ControlGeneratorXmlConfigurationTestDouble("thefile.xml");

            Assert.AreEqual("thefile.xml", config.FileName);
        }

        [TestMethod]
        public void TestNamespace()
        {
            ControlGeneratorXmlConfigurationTestDouble.ConfigurationXml = StandardXml;
            var config = new ControlGeneratorXmlConfigurationTestDouble("thefile.xml");

            Assert.AreEqual("namespace", config.Namespace);
        }

        [TestMethod]
        public void TestOptionalTemplatePartFactory()
        {
            ControlGeneratorXmlConfigurationTestDouble.ConfigurationXml = StandardXml;
            var config = new ControlGeneratorXmlConfigurationTestDouble("thefile.xml");

            Assert.AreEqual("OTPF", config.OptionalTemplatePartFactory);
        }

        [TestMethod]
        public void TestRoutedPropertyEventArgsFactory()
        {
            ControlGeneratorXmlConfigurationTestDouble.ConfigurationXml = StandardXml;
            var config = new ControlGeneratorXmlConfigurationTestDouble("thefile.xml");

            Assert.AreEqual("RPEAF", config.RoutedPropertyEventArgsFactory);
        }

        [TestMethod]
        public void TestTemplatePartFactory()
        {
            ControlGeneratorXmlConfigurationTestDouble.ConfigurationXml = StandardXml;
            var config = new ControlGeneratorXmlConfigurationTestDouble("thefile.xml");

            Assert.AreEqual("TPF", config.TemplatePartFactory);
        }

        [TestMethod]
        public void TestGetControlConfigurationExisting()
        {
            ControlGeneratorXmlConfigurationTestDouble.ConfigurationXml = StandardXml;
            var config = new ControlGeneratorXmlConfigurationTestDouble("thefile.xml");

            var controlConfig = config.GetControlConfiguration("ControlName1");

            Assert.IsNotNull(controlConfig);
            Assert.AreEqual("ControlName1", controlConfig.Name);
        }

        [TestMethod]
        [ExpectedException(typeof(KeyNotFoundException))]
        public void TestGetControlConfigurationNotExisting()
        {
            ControlGeneratorXmlConfigurationTestDouble.ConfigurationXml = StandardXml;
            var config = new ControlGeneratorXmlConfigurationTestDouble("thefile.xml");

            var controlConfig = config.GetControlConfiguration("blahblahblah");

            Assert.IsNotNull(controlConfig);
        }

        [TestMethod]
        [ExpectedException(typeof(KeyNotFoundException))]
        public void TestGetControlConfigurationEmpty()
        {
            ControlGeneratorXmlConfigurationTestDouble.ConfigurationXml = StandardXml;
            var config = new ControlGeneratorXmlConfigurationTestDouble("thefile.xml");

            var controlConfig = config.GetControlConfiguration("");

            Assert.IsNotNull(controlConfig);
        }

        [TestMethod]
        [ExpectedException(typeof(ArgumentNullException))]
        public void TestGetControlConfigurationNull()
        {
            ControlGeneratorXmlConfigurationTestDouble.ConfigurationXml = StandardXml;
            var config = new ControlGeneratorXmlConfigurationTestDouble("thefile.xml");

            var controlConfig = config.GetControlConfiguration(null);

            Assert.IsNotNull(controlConfig);
        }

        [TestMethod]
        public void TestEmptyRoutedPropertyEventArgsFactory()
        {
            ControlGeneratorXmlConfigurationTestDouble.ConfigurationXml = EmptyRoutedPropertyEventArgsFactoryXml;
            var config = new ControlGeneratorXmlConfigurationTestDouble("thefile.xml");

            Assert.AreEqual("", config.RoutedPropertyEventArgsFactory);
        }

        [TestMethod]
        public void TestMissingRoutedPropertyEventArgsFactory()
        {
            ControlGeneratorXmlConfigurationTestDouble.ConfigurationXml = MissingRoutedPropertyEventArgsFactoryXml;
            var config = new ControlGeneratorXmlConfigurationTestDouble("thefile.xml");

            Assert.AreEqual(null, config.RoutedPropertyEventArgsFactory);
        }

        [TestMethod]
        public void TestEmptyTemplatePartFactory()
        {
            ControlGeneratorXmlConfigurationTestDouble.ConfigurationXml = EmptyTemplatePartFactoryXml;
            var config = new ControlGeneratorXmlConfigurationTestDouble("thefile.xml");

            Assert.AreEqual("", config.TemplatePartFactory);
        }

        [TestMethod]
        public void TestMissingTemplatePartFactory()
        {
            ControlGeneratorXmlConfigurationTestDouble.ConfigurationXml = MissingTemplatePartFactoryXml;
            var config = new ControlGeneratorXmlConfigurationTestDouble("thefile.xml");

            Assert.AreEqual(null, config.TemplatePartFactory);
        }

        [TestMethod]
        public void TestEmptyOptionalTemplatePartFactory()
        {
            ControlGeneratorXmlConfigurationTestDouble.ConfigurationXml = EmptyOptionalTemplatePartFactoryXml;
            var config = new ControlGeneratorXmlConfigurationTestDouble("thefile.xml");

            Assert.AreEqual("", config.OptionalTemplatePartFactory);
        }

        [TestMethod]
        public void TestMissingOptionalTemplatePartFactory()
        {
            ControlGeneratorXmlConfigurationTestDouble.ConfigurationXml = MissingOptionalTemplatePartFactoryXml;
            var config = new ControlGeneratorXmlConfigurationTestDouble("thefile.xml");

            Assert.AreEqual(null, config.OptionalTemplatePartFactory);
        }

        [TestMethod]
        public void TestEmptyCompany()
        {
            ControlGeneratorXmlConfigurationTestDouble.ConfigurationXml = EmptyCompanyXml;
            var config = new ControlGeneratorXmlConfigurationTestDouble("thefile.xml");

            Assert.AreEqual("", config.Company);
        }

        [TestMethod]
        public void TestMissingCompany()
        {
            ControlGeneratorXmlConfigurationTestDouble.ConfigurationXml = MissingCompanyXml;
            var config = new ControlGeneratorXmlConfigurationTestDouble("thefile.xml");

            Assert.AreEqual(null, config.Company);
        }

        [TestMethod]
        public void TestEmptyCopyright()
        {
            ControlGeneratorXmlConfigurationTestDouble.ConfigurationXml = EmptyCopyrightXml;
            var config = new ControlGeneratorXmlConfigurationTestDouble("thefile.xml");

            Assert.AreEqual("", config.Copyright);
        }

        [TestMethod]
        public void TestMissingCopyright()
        {
            ControlGeneratorXmlConfigurationTestDouble.ConfigurationXml = MissingCopyrightXml;
            var config = new ControlGeneratorXmlConfigurationTestDouble("thefile.xml");

            Assert.AreEqual(null, config.Copyright);
        }

        [TestMethod]
        public void TestEmptyNamespace()
        {
            ControlGeneratorXmlConfigurationTestDouble.ConfigurationXml = EmptyNamespaceXml;
            var config = new ControlGeneratorXmlConfigurationTestDouble("thefile.xml");

            Assert.AreEqual("", config.Namespace);
        }

        [TestMethod]
        public void TestMissingNamespace()
        {
            ControlGeneratorXmlConfigurationTestDouble.ConfigurationXml = MissingNamespaceXml;
            var config = new ControlGeneratorXmlConfigurationTestDouble("thefile.xml");

            Assert.AreEqual(null, config.Namespace);
        }

        [TestMethod]
        public void TestMissingControls()
        {
            ControlGeneratorXmlConfigurationTestDouble.ConfigurationXml = MissingControlsXml;
            var config = new ControlGeneratorXmlConfigurationTestDouble("thefile.xml");

            Assert.AreEqual(0, config.ControlConfigurations.Count);
        }

    }
}
